<!-- 工艺锁定 -->
<template>
  <div>
    <avue-crud
      :option="option"
      :defaults.sync="defaults"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      v-model="form"
      ref="crud"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="danger" size="small" icon="el-icon-unlock" @click="handleBatchUnLock">解除锁定</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button icon="el-icon-unlock" size="small" type="text" v-if="row.status == 1" @click="unLock(row)" v-preventReClick>解除锁定</el-button>
        <el-button icon="el-icon-view" size="small" type="text" @click="viewDetail(row)">详情</el-button>
      </template>

      <template slot-scope="" slot="objectWorkOrderNoSearch">
        <el-input placeholder="请选择 工单号" readonly="true" v-model="search.objectWorkOrderNo">
          <el-button slot="append" icon="el-icon-search" @click="openObjectDimensionNoModal"></el-button>
        </el-input>
      </template>
    </avue-crud>
    <common-modal-template ref="commonModalTemplate" :option="tableOption" @getSelectedRows="getSelectedRows" :key="timer" @newKey="newKey"></common-modal-template>

    <!-- 工艺锁定详情 -->
    <el-dialog append-to-body :close-on-click-modal="false" title="详情" :visible.sync="viewDialog" width="50%" top="20vh">
      <div class="detail-title">锁定信息</div>
      <table border="1" cellspacing="0" cellpadding="0" style="width: 100%">
        <tbody style="border: 1px solid #ebeef5">
          <tr style="height: 40px">
            <td class="tdStyle">工单号:</td>
            <td style="width: 300px">{{ rowData.objectWorkOrderNo }}</td>
            <td class="tdStyle">产品编码:</td>
            <td style="width: 300px">{{ rowData.objectNo }}</td>
          </tr>
          <tr style="height: 40px">
            <td class="tdStyle">产品名称:</td>
            <td style="width: 300px">{{ rowData.objectName }}</td>
            <td class="tdStyle">产品类别:</td>
            <td style="width: 300px">{{ rowData.categoryName }}</td>
          </tr>
          <tr style="height: 40px">
            <td class="tdStyle">产品型号:</td>
            <td style="width: 300px">{{ rowData.objectModel }}</td>
            <td class="tdStyle">锁定维度:</td>
            <td style="width: 300px" v-if="rowData.lockedType == 'PART_WORKORDER'">工单</td>
            <td style="width: 300px" v-if="rowData.lockedType == 'PART_ROUTE'">
              {{ rowData.lockedObjects.length > 0 && rowData.lockedObjects[0].objectDimensionNameList.join('，') }}
            </td>
          </tr>
          <tr style="height: 40px">
            <td class="tdStyle">锁定范围:</td>
            <td style="width: 300px" v-if="rowData.lockedOperations == '作业'">作业</td>
            <td style="width: 300px" v-else></td>
            <td class="tdStyle">锁定人:</td>
            <td style="width: 300px">{{ rowData.lockedByName }}</td>
          </tr>
          <tr style="height: 40px">
            <td class="tdStyle">锁定时间:</td>
            <td>{{ rowData.lockedTime }}</td>
            <td class="tdStyle">已锁定时间:</td>
            <td>{{ rowData.lockedTimes }}</td>
          </tr>
          <tr style="height: 40px">
            <td class="tdStyle">锁定原因:</td>
            <td colspan="3">{{ rowData.lockedReason }}</td>
          </tr>
        </tbody>
      </table>
      <div class="detail-title" v-if="rowData.status == 2">解锁信息</div>
      <table border="1" cellspacing="0" cellpadding="0" style="width: 100%" v-if="rowData.status == 2">
        <tbody style="border: 1px solid #ebeef5">
          <tr style="height: 40px">
            <td class="tdStyle">解锁人:</td>
            <td style="width: 300px">{{ rowData.unlockedByName }}</td>
            <td class="tdStyle">解锁时间:</td>
            <td style="width: 300px">{{ rowData.unlockedTime }}</td>
          </tr>
          <tr style="height: 40px">
            <td class="tdStyle">解锁原因:</td>
            <td colspan="3">{{ rowData.unlockedRemark }}</td>
          </tr>
        </tbody>
      </table>
    </el-dialog>

    <!-- 解除锁定 -->
    <el-dialog custom-class="unlock-dialog" append-to-body :close-on-click-modal="false" title="解除锁定" :visible.sync="unlockDialog.show" width="50%" top="20vh">
      <avue-form ref="unlockRef" v-model="unlockDialog.form" :option="unlockDialog.option" @submit="handleunLockSubmit">
        <template #selectInfoData>
          <avue-crud :option="unlockData.option" :data="unlockData.data"></avue-crud>
        </template>
      </avue-form>
    </el-dialog>
  </div>
</template>
<script>
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
import { getList, partLock, unLock, getRoutesByWo, batchUnlock } from '@/api/product/part-lock';
import { timeDifference } from '@/util/util';
export default {
  components: {
    CommonModalTemplate
  },
  data() {
    return {
      unLockType: 'single', // 解锁类型:单个解锁还是批量解锁
      rowData: {},
      search: {},
      viewDialog: false,
      option: {
        height: 'calc(100vh - 380px)',
        calcHeight: 30,
        searchShow: true,
        searchSpan: 5,
        searchMenuSpan: 5,
        border: true,
        index: true,
        selection: true,
        dialogClickModal: false,
        addBtn: true,
        addTitle: '新增锁定',
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        tip: false,
        indexLabel: '序号',
        dialogWidth: '50%',
        menuWidth: 150,
        selectable: (row, index) => {
          return row.status === 1;
        },
        column: [
          {
            label: '工单号',
            prop: 'objectWorkOrderNo',
            search: true,
            formslot: true,
            type: 'search',
            width: 140,
            display: false,
            appendClick: () => {
              this.openObjectDimensionNoModal();
            }
          },
          {
            label: '产品编码',
            prop: 'objectNo',
            search: true,
            width: 120,
            display: false
          },
          {
            label: '产品名称',
            prop: 'objectName',
            search: true,
            width: 120,
            display: false
          },
          {
            label: '产品类别',
            prop: 'categoryName',
            search: true,
            width: 120,
            type: 'tree',
            dicUrl: '/api/mes-admin/part-category/tree',
            props: {
              label: 'title',
              value: 'title'
            },
            display: false
          },
          {
            label: '产品型号',
            prop: 'objectModel',
            width: 120,
            display: false
          },

          {
            label: '锁定原因',
            prop: 'lockedReason',
            width: 140,
            display: false,
            overHidden: true
          },
          {
            label: '锁定维度',
            prop: 'lockedType',
            html: true,
            width: 140,
            display: false,
            overHidden: true,
            formatter: (val) => {
              if (val.lockedType === 'PART_WORKORDER') {
                return '<span class="text-green">' + '工单' + '</span>';
              } else if (val.lockedType === 'PART_ROUTE') {
                let lockedObjects = val.lockedObjects;
                if (lockedObjects && lockedObjects.length > 0) {
                  let routeList = lockedObjects[0].objectDimensionNameList.join('，');
                  return '<span class="text-yellow">' + routeList + '</span>';
                }
              }
            }
          },
          {
            label: '锁定范围',
            prop: 'lockedOperations',
            display: false,
            width: 80,
            type: 'checkbox',
            dicData: [
              {
                label: '作业',
                value: 'WORK'
              }
            ]
          },
          {
            label: '锁定人',
            prop: 'lockedByName',
            width: 100,
            search: true,
            display: false
          },
          {
            label: '锁定时间',
            prop: 'lockedTime',
            searchSpan: 8,
            searchType: 'datetimerange',
            search: true,
            width: 130,
            display: false,
            type: 'date',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间'
          },
          {
            label: '已锁定时间',
            width: 130,
            prop: 'lockedTimes',
            display: false
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            width: 60,
            display: false,
            html: true,
            formatter: (val) => {
              switch (val.status) {
                case 2:
                  return `<span class="stateWords-success">已解锁</span>`;
                case 1:
                  return `<span class="stateWords-danger">已锁定</span>`;
                default:
                  break;
              }
            }
          }
        ],
        group: [
          {
            label: '工单信息',
            prop: 'partInfo',
            arrow: false,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                label: '工单号',
                prop: 'objectWorkOrderNo',
                type: 'search',
                disabled: true,
                appendClick: () => {
                  this.openWorkOrderModal();
                },
                rules: [
                  {
                    required: true,
                    message: '请选择工单号',
                    trigger: 'blur'
                  }
                ]
              },
              {
                label: '产品名称',
                prop: 'objectName',
                disabled: true
              },
              {
                label: '产品类别',
                prop: 'categoryName',
                disabled: true
              },
              {
                label: '产品编码',
                prop: 'objectNo',
                disabled: true
              },
              {
                label: '产品型号',
                prop: 'objectModel',
                disabled: true
              }
            ]
          },
          {
            label: '锁定维度',
            prop: 'lockDimension',
            arrow: false,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                label: '工序',
                prop: 'objectDimensionIdList',
                type: 'select',
                dicData: [],
                multiple: true,
                props: {
                  label: 'name',
                  value: 'id'
                },
                span: 24,
                rules: [
                  {
                    required: false,
                    message: '请选择工序',
                    trigger: 'blur'
                  }
                ]
              }
            ]
          },
          {
            label: '锁定范围',
            prop: 'lockedOperations',
            arrow: false,
            icon: 'el-icon-vertical-bar',
            column: [
              {
                label: '',
                labelWidth: 20,
                prop: 'lockedOperations',
                type: 'checkbox',
                row: true,
                value: ['WORK'],
                dicData: [
                  {
                    label: '作业',
                    value: 'WORK'
                  }
                ],
                rules: [
                  {
                    required: true,
                    message: '请选择锁定范围',
                    trigger: 'blur'
                  }
                ]
              }
            ]
          },
          {
            column: [
              {
                label: '锁定原因',
                prop: 'lockedReason',
                type: 'textarea',
                row: true,
                span: 24,
                maxRows: 3,
                minRows: 3,
                rules: [
                  {
                    required: true,
                    message: '请输入锁定原因',
                    trigger: 'blur'
                  }
                ]
              }
            ]
          }
        ]
      },
      unlockDialog: {
        show: false,
        rowData: null,
        form: {
          unlockedRemark: '',
          unlockedLevel: 0
        },
        option: {
          labelWidth: '100',
          submitBtn: true,
          emptyBtn: true,
          menuPosition: 'right',
          group: [
            {
              label: '锁定',
              prop: 'lock',
              arrow: false,
              icon: 'el-icon-vertical-bar',
              column: [
                {
                  label: '解锁方式',
                  prop: 'unlockedLevel',
                  span: 24,
                  type: 'radio',
                  display: true,
                  value: 0,
                  dicData: [
                    {
                      label: '仅解锁工单',
                      value: 0
                    },
                    {
                      label: '解锁工单同时解锁锁定的工序',
                      value: 1
                    }
                  ],
                  rules: [
                    {
                      required: true,
                      message: '请选择解锁方式',
                      trigger: 'change'
                    }
                  ]
                },
                {
                  label: '解锁原因',
                  prop: 'unlockedRemark',
                  span: 24,
                  type: 'textarea',
                  showWordLimit: true,
                  maxlength: 200,
                  rules: [
                    {
                      required: true,
                      message: '请输入解锁原因',
                      trigger: 'blur'
                    }
                  ]
                }
              ]
            },
            {
              label: '选中信息',
              prop: 'selectInfo',
              arrow: false,
              icon: 'el-icon-vertical-bar',
              column: [
                {
                  label: '',
                  labelWidth: 0,
                  prop: 'selectInfoData',
                  span: 24,
                  formslot: true
                }
              ]
            }
          ]
        }
      },
      unlockData: {
        option: {
          height: '200px',
          calcHeight: 30,
          searchShow: true,
          searchSpan: 5,
          searchMenuSpan: 5,
          border: true,
          index: true,
          selection: false,
          dialogClickModal: false,
          addBtn: false,
          viewBtn: false,
          editBtn: false,
          delBtn: true,
          refreshBtn: false,
          columnBtn: false,
          tip: false,
          indexLabel: '序号',
          dialogWidth: '50%',
          menu: false,
          column: [
            {
              label: '产品编码',
              prop: 'objectNo'
            },
            {
              label: '产品名称',
              prop: 'objectName'
            },
            {
              label: '产品型号',
              prop: 'objectModel',
              overHidden: true
            },
            {
              label: '工单号',
              prop: 'objectWorkOrderNo',
              overHidden: true
            },
            {
              label: '锁定维度',
              prop: 'lockedType',
              overHidden: true,
              html: true,
              formatter: (val) => {
                if (val.lockedType === 'PART_WORKORDER') {
                  return '<span class="text-green">' + '工单' + '</span>';
                } else if (val.lockedType === 'PART_ROUTE') {
                  let lockedObjects = val.lockedObjects;
                  if (lockedObjects && lockedObjects.length > 0) {
                    let routeList = lockedObjects[0].objectDimensionNameList.join('，');
                    return '<span class="text-yellow">' + routeList + '</span>';
                  }
                }
              }
            },
            {
              label: '锁定范围',
              prop: 'lockedOperations',
              overHidden: true
            },
            {
              label: '锁定原因',
              prop: 'lockedReason',
              overHidden: true
            }
          ]
        },
        data: []
      },
      routeList: [],
      rowInfo: {},

      defaults: {},
      loading: true,
      data: [],
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      form: {
        objectDimensionIdList: []
      },
      query: {},
      selectionList: [],
      modalName: '',
      tableOption: {},
      timer: new Date().getTime() //不同内容使用同一个模态框结构，key值不同需要重新请求渲染数据，否则会请求到上一个使用的数据内容
    };
  },
  methods: {
    searchChange(params, done) {
      if (params.lockedTime && params.lockedTime.length > 0) {
        params.lockedTimeStart = params.lockedTime[0];
        params.lockedTimeEnd = params.lockedTime[1];
        delete params.lockedTime;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      this.query['lockedType'] = 'PART_WORKORDER,PART_ROUTE';
      // this.query['status'] = 1;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this._forEach(this.data, (item) => {
          item.lockedObjects = JSON.parse(item.lockedObjects);
          item['objectNo'] = this._get(item, 'lockedObjects[0].objectNo', '');
          item['objectName'] = this._get(item, 'lockedObjects[0].objectName', '');
          item['categoryName'] = this._get(item, 'lockedObjects[0].categoryName', '');
          item['objectModel'] = this._get(item, 'lockedObjects[0].objectModel', '');
          item['objectDimensionName'] = this._get(item, 'lockedObjects[0].objectDimensionName', '');
          item['objectWorkOrderNo'] = this._get(item, 'lockedObjects[0].objectWorkOrderNo', '');
          item['objectDimensionLotNo'] = this._get(item, 'lockedObjects[0].objectDimensionLotNo', '');
          let lockedOperations = item.lockedOperations.split(',');
          let arr = [];
          this._forEach(lockedOperations, (lockedOperationsItem) => {
            if (lockedOperationsItem === 'WORK') {
              arr.push('作业');
            }
          });
          item.lockedOperations = arr.toString();
          item.lockRange = item.lockedOperations; // 锁定范围
          if (item.status == 1) {
            item['lockedTimes'] = timeDifference(item.lockedTime, this.$moment(new Date()).add(5, 'seconds'));
          } else {
            item['lockedTimes'] = timeDifference(item.lockedTime, item.unlockedTime);
          }
        });
        this.loading = false;
        this.selectionClear();
      });
    },
    // 打开顶部搜索工单号
    openObjectDimensionNoModal() {
      this.$refs.commonModalTemplate.showModal();
      this.modalName = 'searchWorkOrderModal';
      this.tableOption = {
        modalTitle: '选择工单号（双击进行选择）',
        url: '/api/mes-product/work-order/page',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '生产订单',
            prop: 'productionOrderNo',
            search: true
          },
          {
            label: '生产工单',
            prop: 'orderNo',
            search: true
          },
          {
            label: '工单类型',
            prop: 'orderType',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=order_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '工单数量',
            prop: 'planQuantity'
          },
          {
            label: '产品编码',
            prop: 'partNo'
          },
          {
            label: '产品名称',
            prop: 'partName'
          },
          {
            label: 'BOM版本',
            prop: 'bomVersion'
          },
          {
            label: '派工产线',
            prop: 'productionLineName'
          },
          {
            label: '计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '计划结束时间',
            prop: 'planEndDate'
          },
          {
            label: '制定人',
            prop: 'creatorName'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            dicData: [
              {
                label: '待排程',
                value: 1
              },
              {
                label: '待生产',
                value: 2
              },
              {
                label: '生产中',
                value: 3
              },
              {
                label: '完工',
                value: 4
              },
              {
                label: '锁定',
                value: 5
              },
              {
                label: '强制完工',
                value: 6
              }
            ]
          },
          {
            label: '备注',
            prop: 'remark'
          }
        ]
      };
    },
    /**
     * 打开工单选择模态框
     */
    openWorkOrderModal() {
      this.$refs.commonModalTemplate.showModal();
      this.modalName = 'workOrderModal';
      this.tableOption = {
        modalTitle: '选择工单信息（双击进行选择）',
        url: `/api/mes-product/work-order/page`,
        isPage: true,
        index: false,
        rowSelectionType: 'single',
        searchMenuSpan: 24,
        columnDefs: [
          {
            label: '生产订单',
            prop: 'productionOrderNo',
            search: true,
            searchType: 'input'
          },
          {
            label: '生产工单',
            prop: 'orderNo',
            search: true
          },
          {
            label: '工单类型',
            prop: 'orderType',
            dicUrl: '/api/blade-system/dict/dictionary?code=order_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '工单数量',
            prop: 'planQuantity'
          },
          {
            label: '产品编码',
            prop: 'partNo',
            searchType: 'input'
          },
          {
            label: '产品名称',
            prop: 'partName'
          },
          {
            label: '产品类别',
            prop: 'categoryName'
          },
          {
            label: 'BOM版本',
            prop: 'bomVersion'
          },
          {
            label: '派工产线',
            prop: 'productionLineName',
            search: true,
            searchType: 'input'
          },
          {
            label: '计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '计划结束时间',
            prop: 'planEndDate'
          },
          {
            label: '制定人',
            prop: 'creatorName'
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            search: true,
            dicData: [
              {
                label: '待排程',
                value: 1
              },
              {
                label: '待生产',
                value: 2
              },
              {
                label: '生产中',
                value: 3
              },
              {
                label: '完工',
                value: 4
              },
              {
                label: '锁定',
                value: 5
              },
              {
                label: '强制完工',
                value: 6
              }
            ]
          }
        ]
      };
    },
    // 获取选择的模态框行数据
    getSelectedRows(rowData) {
      switch (this.modalName) {
        case 'workOrderModal':
          if (rowData.status == '5') {
            this.$message.warning('此工单已锁定，请选择其它工单');
            return false;
          }
          this.form.objectDimensionId = this._get(rowData, 'id', '');
          this.form.objectWorkOrderNo = this._get(rowData, 'orderNo', '');
          this.form.objectDimensionName = this._get(rowData, 'name', '');
          this.form.objectName = this._get(rowData, 'partName', '');
          this.form.objectNo = this._get(rowData, 'partNo', '');
          this.form.objectModel = this._get(rowData, 'partModel', '');
          this.form.categoryName = this._get(rowData, 'categoryName', '');
          this.form.objectDimensionIdList = [];
          this.$loading({
            text: '查找工序中，请稍后'
          });
          getRoutesByWo(this.form.objectDimensionId)
            .then((res) => {
              let data = res.data.data;
              this.routeList = data;
              this.option.group[1].column[0].dicData = data;
            })
            .catch(() => {})
            .finally(() => {
              this.$loading().close();
            });
          break;
        case 'searchWorkOrderModal':
          this.search.objectWorkOrderNo = this._get(rowData, 'orderNo', '');
        default:
          break;
      }
    },
    // 重新加载公用表格弹窗组件
    newKey() {
      this.timer = new Date().getTime();
    },
    rowSave(row, done, loading) {
      let message = '';
      let routeNames = '';
      let objectDimensionNameList = [];
      let lockedType = row.objectDimensionIdList.length == 0 ? 'PART_WORKORDER' : 'PART_ROUTE';
      if (lockedType == 'PART_WORKORDER') {
        message = `是否要锁定工单：${row.objectWorkOrderNo}?`;
      } else {
        if (row.$objectDimensionIdList) {
          routeNames = row.$objectDimensionIdList.split('|');
          for (let i = 0; i < routeNames.length; i++) {
            routeNames[i] = routeNames[i].trim();
          }
          objectDimensionNameList = routeNames;
          routeNames = routeNames.join(',');
          message = `是否要锁定工单：${row.objectWorkOrderNo}的工序：${routeNames}?`;
        }
      }
      this.routeList = this.routeList
        .filter((route) => {
          return row.objectDimensionIdList.includes(route.id);
        })
        .map((r) => r.name);
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let lockedObjects = {
            objectId: row.objectId,
            objectNo: row.objectNo,
            objectName: row.objectName,
            objectModel: row.objectModel,
            objectId: row.objectDimensionId,
            objectWorkOrderNo: row.objectWorkOrderNo,
            objectDimensionName: row.objectDimensionName,
            objectDimensionIdList: row.objectDimensionIdList,
            objectDimensionNameList: objectDimensionNameList,
            categoryName: row.categoryName
          };
          let params = {
            lockedType: lockedType,
            lockedObjects: JSON.stringify([lockedObjects]),
            lockedOperations: row.lockedOperations.join(','),
            lockedReason: row.lockedReason,
            objectWorkOrderNo: row.objectWorkOrderNo,
            objectDimensionNames: routeNames
          };
          console.log('params', params);
          partLock(params).then(
            (res) => {
              this.onLoad(this.page);
              this.$message({
                type: 'success',
                message: res.data.msg
              });
              done();
            },
            () => {
              loading();
            }
          );
        })
        .catch(() => {
          loading();
        });
    },
    handleBatchUnLock() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return false;
      }
      // 锁定维度是否统一
      let lockedTypeArray = this.selectionList.map((item) => item.lockedType);
      let isUnify = lockedTypeArray.includes('PART_WORKORDER') && lockedTypeArray.includes('PART_ROUTE');

      if (isUnify) {
        this.$message.warning('所选明细不属于同一锁定维度！只有同一锁定维度（工单/工序）才能进行批量解锁');
        return false;
      }
      this.unLockType = 'batch';
      this.unlockDialog.show = true;
      this.unlockDialog.form.unlockedRemark = '';
      this.unlockDialog.form.unlockedLevel = 0;

      let batchData = structuredClone(this.selectionList);
      console.log('batchData', batchData);
      this.handleLockType(batchData[0].lockedType);

      let result = [];
      batchData.map((item) => {
        result.push({
          objectNo: item.objectNo,
          objectName: item.objectName,
          objectModel: item.objectModel,
          objectWorkOrderNo: item.objectWorkOrderNo,
          lockedType: item.lockedType,
          lockedObjects: item.lockedObjects,
          lockedOperations: item.lockRange,
          lockedReason: item.lockedReason
        });
      });
      this.unlockData.data = result;
    },
    // 操作-解除锁定
    unLock(row, done) {
      this.unLockType = 'single';
      this.unlockDialog.show = true;
      this.unlockDialog.form.unlockedRemark = '';
      this.unlockDialog.form.unlockedLevel = 0;
      this.rowInfo = JSON.parse(JSON.stringify(row));

      this.handleLockType(this.rowInfo.lockedType);

      this.unlockData.data = [
        {
          objectNo: this.rowInfo.objectNo,
          objectName: this.rowInfo.objectName,
          objectModel: this.rowInfo.objectModel,
          objectWorkOrderNo: this.rowInfo.objectWorkOrderNo,
          lockedType: this.rowInfo.lockedType,
          lockedObjects: this.rowInfo.lockedObjects,
          lockedOperations: this.rowInfo.lockRange,
          lockedReason: this.rowInfo.lockedReason
        }
      ];
    },
    // 锁定方式
    handleLockType(data) {
      let unlockedLevelColumnIndex = this.unlockDialog.option.group[0].column.findIndex((col) => col.prop == 'unlockedLevel');
      if (data == 'PART_WORKORDER') {
        this.unlockDialog.option.group[0].column[unlockedLevelColumnIndex].display = true;
      } else if (data == 'PART_ROUTE') {
        this.unlockDialog.option.group[0].column[unlockedLevelColumnIndex].display = false;
      }
    },
    beforeClose(done) {
      done();
    },
    // 解除锁定确认
    handleunLockSubmit(form, done) {
      if (this.unLockType == 'single') {
        let msg = '';
        if (this.rowInfo.lockedType == 'PART_ROUTE') {
          let routedList = this.rowInfo.lockedObjects;
          if (routedList && routedList.length > 0) {
            if (routedList[0].objectDimensionNameList) {
              routedList = routedList[0].objectDimensionNameList.join(',');
              msg = `确定将工单：${this.rowInfo.objectWorkOrderNo}的工序：${routedList}解除锁定?`;
            }
          }
          this.$confirm(msg, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              unLock({
                id: this.rowInfo.id,
                unlockedRemark: form.unlockedRemark
              })
                .then((res) => {
                  this.unlockDialog.show = false;
                  this.onLoad(this.page);
                  this.$message({
                    type: 'success',
                    message: res.data.msg
                  });
                })
                .finally(() => done());
            })
            .catch(() => done());
        }
        if (this.rowInfo.lockedType == 'PART_WORKORDER') {
          msg = `确定将工单${this.rowInfo.objectWorkOrderNo}解除锁定?`;
          this.$confirm(msg, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(() => {
              unLock({
                id: this.rowInfo.id,
                unlockedRemark: form.unlockedRemark,
                unlockedLevel: form.unlockedLevel
              })
                .then((res) => {
                  this.unlockDialog.show = false;
                  this.onLoad(this.page);
                  this.$message({
                    type: 'success',
                    message: res.data.msg
                  });
                })
                .finally(() => done());
            })
            .catch(() => done());
        }
      } else {
        this.handleBatchUnLockSubmit(form, done);
      }
    },
    handleBatchUnLockSubmit(form, done) {
      if (this.selectionList[0].lockedType == 'PART_ROUTE') {
        batchUnlock({
          lockedType: 'PART_PRODUCT',
          unlockedRemark: form.unlockedRemark,
          idList: this.selectionList.map((item) => item.id)
        })
          .then(() => {
            this.unlockDialog.show = false;
            this.onLoad(this.page);
            this.$message({
              type: 'success',
              message: res.data.msg
            });
            this.selectionList = [];
            this.$refs.crud && this.$refs.crud.toggleSelection();
          })
          .catch(() => {})
          .finally(() => {
            done();
          });
      } else {
        batchUnlock({
          lockedType: 'PART_WORKORDER',
          unlockedLevel: form.unlockedLevel,
          unlockedRemark: form.unlockedRemark,
          idList: this.selectionList.map((item) => item.id)
        })
          .then(() => {
            this.unlockDialog.show = false;
            this.onLoad(this.page);
            this.$message({
              type: 'success',
              message: res.data.msg
            });
            this.selectionList = [];
            this.$refs.crud && this.$refs.crud.toggleSelection();
          })
          .catch(() => {})
          .finally(() => {
            done();
          });
      }
    },
    beforeOpen(done, type) {
      if (['add'].includes(type)) {
        this.option.group[1].column[0].dicData = [];
      }
      done();
    },
    viewDetail(row) {
      this.rowData = row; // 基本信息
      this.viewDialog = true;
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-table {
  margin-bottom: 10px;
}
.tdStyle {
  background: #fafafa;
  font-weight: 700;
  color: #909399;
  text-align: right;
  width: auto;
  padding-right: 20px;
  white-space: nowrap;
}
td {
  padding: 5px;
  border: 1px solid #ebeef5;
}
.detail-title {
  font-weight: bold;
  font-size: 14px;
  margin: 10px 0;
}
::v-deep .unlock-dialog {
  .el-dialog__body {
    padding-top: 0;
    .avue-crud__menu {
      display: none;
    }
  }
}
</style>
